<?php
namespace app\components\printMachine\feie\formatter;

abstract class Formatter {

    /**
     * 格式化
     * @param array $header 头数据
     * @param array $arr 菜单数据
     * @return mixed
     */
    public abstract function format($header,$arr);

    /**
     *  排版
     *  #########################################################################################################
     *  进行订单的多列排版demo，实现商品超出字数的自动换下一行对齐处理，同时保持各列进行对齐
     *  排版原理是统计字符串字节数，补空格换行处理
     *  58mm的机器,一行打印16个汉字,32个字母;80mm的机器,一行打印24个汉字,48个字母
     *  名称14 单价6 数量3 金额6-->这里的字节数可按自己需求自由改写，14+6+3+6再加上代码写的3个空格就是32了
     *  #########################################################################################################
     * @param array $header 头数据
     * @param array $arr 配货数据
     * @param int $A 名称字节数
     * @param int $B 单价
     * @param int $C 数量
     * @param int $D 金额
     * @return string
     */
    protected function compose(array $header,$arr,$A,$B,$C,$D){
        $orderInfo = '<CB>'.$header['merchant_name'].'</CB><BR>';
        $orderInfo .= $header['title'];
        $orderInfo .= $header['separator'];
        foreach ($arr as $k5 => $v5) {
            $name = $v5['title'];
            $price = $v5['price'];
            $num = $v5['num'];
            $prices = $price * $v5['num'];
            $kw3 = '';
            $kw1 = '';
            $kw2 = '';
            $kw4 = '';
            $str = $name;
            $blankNum = $A;//名称控制为14个字节
            $lan = mb_strlen($str, 'utf-8');
            $m = 0;
            $j = 1;
            $blankNum++;
            $result = array();
            if (strlen($price) < $B) {
                $k1 = $B - strlen($price);
                for ($q = 0; $q < $k1; $q++) {
                    $kw1 .= ' ';
                }
                $price = $price . $kw1;
            }
            if (strlen($num) < $C) {
                $k2 = $C - strlen($num);
                for ($q = 0; $q < $k2; $q++) {
                    $kw2 .= ' ';
                }
                $num = $num . $kw2;
            }
            if (strlen($prices) < $D) {
                $k3 = $D - strlen($prices);
                for ($q = 0; $q < $k3; $q++) {
                    $kw4 .= ' ';
                }
                $prices = $prices . $kw4;
            }
            for ($i = 0; $i < $lan; $i++) {
                $new = mb_substr($str, $m, $j, 'utf-8');
                $j++;
                if (mb_strwidth($new, 'utf-8') < $blankNum) {
                    if ($m + $j > $lan) {
                        $m = $m + $j;
                        $tail = $new;
                        $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
                        $k = $A - strlen($lenght);
                        for ($q = 0; $q < $k; $q++) {
                            $kw3 .= ' ';
                        }
                        if ($m == $j) {
                            $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
                        } else {
                            $tail .= $kw3 . '<BR>';
                        }
                        break;
                    } else {
                        $next_new = mb_substr($str, $m, $j, 'utf-8');
                        if (mb_strwidth($next_new, 'utf-8') < $blankNum) continue;
                        else {
                            $m = $i + 1;
                            $result[] = $new;
                            $j = 1;
                        }
                    }
                }
            }
            $head = '';
            foreach ($result as $key => $value) {
                if ($key < 1) {
                    $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
                    $v_lenght = strlen($v_lenght);
                    if ($v_lenght == 13) $value = $value . " ";
                    $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
                } else {
                    $head .= $value . '<BR>';
                }
            }
            $orderInfo .= $head . $tail;
        }
        $orderInfo .= $header['separator'];
        $orderInfo .= '总金额：' . $header['total_price'] . ' CZK<BR>';
        $orderInfo .= '优惠金额：' . $header['coupon_price'] . 'CZK<BR>';
        $orderInfo .= '实付金额：' . $header['real_price'] . 'CZK<BR>';
        $orderInfo .= '配送费：' . $header['delivery_price'] . 'CZK<BR>';
        $orderInfo .= '配送方：' . $header['delivery_type'] . '<BR>';
        $orderInfo .= '配送时间：' . $header['delivery_time'] . '<BR>';
        $orderInfo .= '联系人：'.$header['name'].'<BR>';
        $orderInfo .= '联系电话：'.$header['mobile'].'<BR>';
        $orderInfo .= '送货地点：'.$header['address'].'<BR>';
        $orderInfo .= '下单时间：' . $header['add_time'] . '<BR>';
        $orderInfo .= '订单号：' . $header['order_no'] . '<BR>';
        $orderInfo .= '支付方式：' . $header['pay_method'] . '<BR>';
        $orderInfo .= '备注：'.$header['remark'].'<BR><BR>';
        return $orderInfo;
    }
}